package org.skyeye.modeling.rpc.dao;

import java.util.HashMap;
import java.util.Map;

import org.skyeye.modeling.rpc.RPCClient;
import org.skyeye.modeling.rpc.ResponseBean;
import org.skyeye.modeling.rpc.request.Request;
import org.skyeye.modeling.rpc.util.JsonUtil;

import com.alibaba.fastjson.JSONObject;

public class SkyeyeJsonDAOImpl implements SkyeyeJsonDAO {

	private Request request;
	private Map<String, String> args;
	private RPCClient rpcClient;

	public SkyeyeJsonDAOImpl(RPCClient rpcClient) {
		this.rpcClient = rpcClient;
	}

	private ResponseBean run(String name) {
		request = new Request();
		request.setName(name);
		JSONObject reponse = null;
		try {
			System.out.println("[request:]" + "[" + request + "]");
			if (request != null) {
				reponse = rpcClient.run(request.toString(), 60);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("[reponse:]" + "[" + reponse + "]");
		return JsonUtil.parseResult(reponse);
	}
	
	private ResponseBean run(String name, Map<String, String> a) {
		request = new Request();
		request.setName(name);
		request.setArgs(a);
		JSONObject reponse = null;
		try {
			System.out.println("[request:]" + "[" + request + "]");
			if (request != null) {
				reponse = rpcClient.run(request.toString(), 60);
			}
		} catch (Exception  e) {
			e.printStackTrace();
		}
		System.out.println("[reponse:]" + "[" + reponse + "]");
		return JsonUtil.parseResult(reponse);
	}

	

	@Override
	public ResponseBean getFaultInjectDeviceInfo() {
		return run("SE_get_fault_inject_device_info");
	}

	@Override
	public ResponseBean getDeviceRegisterInfo(String machname, String devname) {
		args = new HashMap();
		args.put("machname", machname);
		args.put("devname", devname);
		return run("SE_get_device_register_info", args);
	}

	@Override
	public ResponseBean getFaultInjectList() {
		return run("SE_get_fault_inject_list");
	}

	@Override
	public boolean setFaultInject(String machname, String devname,
			String addr, String bit, String mode) {
		args = new HashMap();
		args.put("machname", machname);
		args.put("devname", devname);
		args.put("addr", addr);
		args.put("bit", bit);
		args.put("mode", mode);
		ResponseBean result = run("SE_set_fault_inject", args);
		return result.isSuccess();
	}

	@Override
	public boolean deleteFault(String machname, String devname, String addr,
			String bit, String mode) {
		args = new HashMap();
		args.put("machname", machname);
		args.put("devname", devname);
		args.put("addr", addr);
		args.put("bit", bit);
		args.put("mode", mode);
		ResponseBean result = run("SE_delete_fault", args);
		return result.isSuccess();
	}

	@Override
	public ResponseBean getCpuList() {
		ResponseBean result = run("cpu_list");
		return result;
	}

	@Override
	public boolean coverageInit() {
		ResponseBean result = run("SE_coverage_init");
		return result.isSuccess();
	}

	@Override
	public ResponseBean countTotalCov() {
		ResponseBean result = run("SE_count_total_cov");
		return result;
	}
	

	@Override
	public ResponseBean countAllFuncCov() {
		ResponseBean result = run("SE_count_total_cov");
		return result;
	}

	@Override
	public ResponseBean countSingleFuncCov() {
		return run("SE_count_total_cov");
	}

	@Override
	public ResponseBean codeRunningNumber() {
		return run("code_running_number");
	}

	@Override
	public ResponseBean codeRunningPath() {
		return run("code_running_path");
	}
	
	
	
	
}
